import wx
from inspect import *
from pyeasyaccept.gui.Controller import Controller
from pyeasyaccept.gui.ButtonCmdsPanel import ButtonCmdsPanel
from pyeasyaccept.commom.Configuration import Configuration

class PyEasyAcceptCmdsPanel(ButtonCmdsPanel):
    """
    It's a panel which contains commands of Python EasyAccept
    Facade. Each button represents a command.
     
    @author: Danilo Torres
    """
    
    COMMENT_DOC = "Adds a comment code #"
    COMMENT = Configuration.COMMENT_CODE
    
    def __init__(self, parent, id):
         """
         Initializes the panel.
         """        
         ButtonCmdsPanel.__init__(self, parent, id)
         self.controller = Controller()
         self.loadPyEasyAcceptCmds()
         self.Layout()
         
    def loadPyEasyAcceptCmds(self):
        """
        It gets each command of Python EasyAccept and put it in a button.
        """
        commandsStr = self.controller.getPyEasyAcceptCmdsStr()
        self.buttons = []
        for cmd in commandsStr:
            
            button = wx.Button(self, self.lastButtonID, cmd)
            cmdInstance = self.controller.getPyEasyAcceptCommand(cmd)
            doc = getdoc(cmdInstance)
            if doc != None: button.SetToolTip(wx.ToolTip(doc))
            button.Bind(wx.EVT_BUTTON, self.buttonPressed)
            self.buttons.append(button)
            self.sizer.Add(button, (self.lastButtonID, 0), flag=wx.EXPAND)
            self.incrementLastButtonID()
        
        ### adding a button for Comments
        commentButton = wx.Button(self, self.lastButtonID, "comment")
        commentButton.SetToolTip(wx.ToolTip(self.COMMENT_DOC))
        commentButton.Bind(wx.EVT_BUTTON, self.buttonPressed)
        self.buttons.append(commentButton)
        self.sizer.Add(commentButton, (self.lastButtonID, 0), flag=wx.EXPAND)
        self.commentID = self.lastButtonID
        self.incrementLastButtonID()
        
            
            
    def buttonPressed(self, event):
        """
        Event of button pressed. 
        """
        id = event.GetId()
        if id == self.commentID:
            cmd = self.COMMENT
            args = []
        else:    
            cmd = self.buttons.__getitem__(id).GetLabelText()
            args = self.getCommandArgs(cmd)
        self.fireButtonPressed(cmd, args)
            
    def fireButtonPressed(self, cmd, args):
        """
        Notify listeners that a PyEasyAccept command button was pressed. 
        """
        for listener in self.listeners:
            listener.buttonPressed(cmd, args)
            
    def getCommandArgs(self, cmd):
        """
        Returns the arguments names of a given command.
        """
        cmdInstance = self.controller.getPyEasyAcceptCommand(cmd)
        return cmdInstance.getParametersNames()
